// Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
// This source file is part of the Cangjie project, licensed under Apache-2.0
// with Runtime Library Exception.
//
// See https://cangjie-lang.cn/pages/LICENSE for license information.

#include "Rules/Rule.h"

namespace CangjieMigrator::Rules {

std::unique_ptr<Rule> Rule::TryCreate(const TomlConfig &config)
{
    auto matchRule = MatchRule::TryCreate(config);
    if (!matchRule) {
        return nullptr;
    }

    auto actionRule = ActionRule(config);

    auto importRule = ImportRule(config);

    return std::make_unique<Rule>(config["reference"],
        std::move(matchRule), std::move(actionRule), std::move(importRule));
}

Rule::Rule(std::string reference, std::unique_ptr<class MatchRule> matchRule,
    ActionRule actionRule, ImportRule importRule)
    : reference(std::move(reference)), matchRule(std::move(matchRule)),
    actionRule(std::move(actionRule)), importRule(std::move(importRule))
{}

std::vector<Actions::Action> Rule::GenerateAction(UpdateInstance::File &file, const ExtractedInfo &info) const
{
    return actionRule.GenerateAction(file, info, *this);
}

} // namespace CangjieMigrator::Rules
